﻿#region 命名空间

using System;
using System.Data;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Text;
using WinnerCMS.BLL;
using WinnerCMS.Common;
using WinnerCMS.Data;
using WinnerCMS.Model;
using WinnerCMS.BLL.PageFactory.HtmlFactory;

#endregion

namespace WinnerCMS.BLL.PageFactory
{
    /// <summary>
    /// 要生成的URL列表
    /// </summary>
    public class MakingUrlList
    {
        private static readonly CoreDal MyDAL = new CoreDal();

        #region 生成内容页需要的Url和静态文件路径

        public IList<string> MakeContentPage(byte modelId)
        {
            IList<Core> list = new GetInfoForMakeHTML().ReadTopContent(modelId);

            return ContentPage(list, modelId);
        }

        /// <summary>
        /// 生成内容页需要的Url和静态文件路径
        /// </summary>
        /// <param name="top"></param>
        /// <param name="modelId"></param>
        /// <returns></returns>
        public IList<string> MakeContentPage(int top, byte modelId)
        {
            IList<Core> list = new GetInfoForMakeHTML().ReadTopContent(modelId, top);

            return ContentPage(list, modelId);
        }

        public IList<string> MakeContentPage(int minId, int maxId, byte modelId)
        {
            IList<Core> list = new GetInfoForMakeHTML().ReadContent(modelId, minId, maxId);

            return ContentPage(list, modelId);
        }

        public IList<string> MakeContentPage(DateTime minDate, DateTime maxDate, byte modelId)
        {
            IList<Core> list = new GetInfoForMakeHTML().ReadContent(modelId, minDate, maxDate);

            return ContentPage(list, modelId);
        }

        /// <summary>
        /// 生成内容页需要的Url和静态文件路径
        /// </summary>
        /// <param name="list"></param>
        /// <param name="modelId"></param>
        /// <returns></returns>
        private IList<string> ContentPage(IEnumerable<Core> list, byte modelId)
        {
            IList<string> urllist = new List<string>();

            Dictionary<int, int> urlStyleDir = MyDAL.ContentUrlStyle(modelId);

            Template tinfo = TemplatesManage.GetUseing;

            foreach (Core model in list)
            {
                int urlStyle;
                urlStyleDir.TryGetValue(model.CategoryId, out urlStyle);
                if (urlStyle == 0)
                {
                    continue; //如果该栏目下面的内容页都是动态的就跳过生成
                }
                if (model.TemplateFileName.Length == 0)
                {
                    continue; //没有设置模板就跳过生成
                }
                 
                urllist.Add(BLL.PageFactory.StaticFilePath.ContentShowUrl(model, 1) + "?Method=Save");
            }


            return urllist;
        }

        #endregion

        #region 生成栏目页需要的Url和静态文件路径

        /// <summary>
        /// 生成栏目页需要的Url和静态文件路径
        /// </summary>
        /// <param name="modelId"></param>
        /// <returns></returns>
        public IList<string> MakeCategoryPage(byte modelId)
        {
            IList<Category> classList = MyDAL.ReadCategory(modelId);

            return MakeCategoryPage(classList, modelId);
        }

        /// <summary>
        /// 生成栏目页需要的Url和静态文件路径
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="modelId"></param>
        /// <returns></returns>
        public IList<string> MakeCategoryPage(string ids, byte modelId)
        {
            if (string.IsNullOrWhiteSpace(ids))
            {
                return MakeCategoryPage(modelId);
            }
            IList<Category> classList = MyDAL.ReadCategory(MyConvert.ConvertToArr(ids));

            return MakeCategoryPage(classList, modelId);
        }

        /// <summary>
        /// 生成栏目页需要的Url和静态文件路径
        /// </summary>
        /// <param name="categoryList"></param>
        /// <param name="modelId"></param>
        /// <returns></returns>
        private IList<string> MakeCategoryPage(IList<Category> categoryList, byte modelId)
        {
            Dictionary<int, int> dic = new Dictionary<int, int>();
            var mm = ModelManage.GetInfo(modelId);
            Template tinfo = TemplatesManage.GetUseing;
            foreach (Category model in categoryList)
            {
                int inPut = MyDAL.CountCategoryNo(model.Id); //栏目和它的子栏目的文章数量

                if (inPut == 0) //解决没有数据的时候，该栏目就不能生成HTML的错误
                {
                    inPut = 1;
                }
                dic.Add(model.Id, inPut);
            }

            IList<string> urllist = new List<string>();

            foreach (Category model in categoryList)
            {
                if (model.CategoryUrlStyle == 0)
                {
                    continue;
                }
                int inPut;
                dic.TryGetValue(model.Id, out inPut);

                int maxPageNo = MyMath.GetCeiling(inPut, mm.PageSize) + 1;
                for (int pageNo = 1; pageNo < maxPageNo; pageNo++)
                {
                    if (pageNo > mm.MaxCategoryPageNo && mm.MaxCategoryPageNo > 0)
                    {
                        //根据优化设置跳过生成
                        break;
                    }


                    urllist.Add(string.Concat(WebConfig.DomainName, "/Templates/", tinfo.TemplatePath,
                        model.TemplateFileName, "?CategoryId=", model.Id, "&PageNo=", pageNo, "&Method=Save"));
                }
            }
            return urllist;
        }

        #endregion

        #region 生成专题页需要的Url和静态文件路径

        /// <summary>
        /// 生成专题页需要的Url和静态文件路径
        /// </summary>
        /// <param name="where"></param>
        /// <param name="modelId"></param>
        /// <returns></returns>
        public IList<string> MakeSpecialPage(string @where, byte modelId)
        {
            IList<Special> specialList = MyDAL.ReadSpecial(@where);
            Dictionary<int, int> dic = new Dictionary<int, int>();

            int pageSize = ModelManage.GetPageSize(modelId);
            foreach (Special model in specialList)
            {
                int inPut = 0; //栏目和它的子栏目的文章数量

                if (inPut == 0) //解决没有数据的时候，该栏目就不能生成HTML的错误
                {
                    inPut = 1;
                }
                dic.Add(model.Id, inPut);
            }

            IList<string> list = new List<string>();

            foreach (Special model in specialList)
            {
                int inPut;
                dic.TryGetValue(model.Id, out inPut);

                int maxPageNo = MyMath.GetCeiling(inPut, pageSize) + 1;

                for (int iPageNo = 1; iPageNo < maxPageNo; iPageNo++)
                {
                    list.Add(string.Format("{0}{1}?ModelId={2}&SpecialID={3}&PageNo={4}&Method=Save",
                        WebConfig.DomainName, model.TemplateFileName, modelId, model.Id, iPageNo));
                }
            }
            return list;
        }

        #endregion

        public string IndexPage()
        {
            string url = string.Format("{0}/Templates/{1}/Index.aspx?Method=Save",
                WebConfig.DomainName,
                TemplatesManage.GetUseing.TemplatePath);
            return url;
        }

        /// <summary>
        /// 模型的所有网页
        /// </summary>
        /// <param name="modelId"></param>
        /// <returns></returns>
        public IList<string> All(byte modelId)
        {
            List<string> url = new List<string>();
            url.AddRange(MakeContentPage(modelId));
            url.AddRange(MakeCategoryPage(" ModelId=" + modelId, modelId));
            url.AddRange(MakeSpecialPage(" ModelId=" + modelId, modelId));
            url.Add(IndexPage());

            return url;
        }
    }
}